﻿Imports DAO
Imports DTO
Imports System.Security.Cryptography


Public Class DocGia_BUS

    Public Shared Function ktGiaTriDocGia(ByVal dg As DocGia_DTO) As Boolean
        If dg.tenDG = "" Then
            Throw New Exception("Tên độc giả không được bỏ trống!")
        End If
        For Each c As Char In dg.tenDG
            If Char.IsLetter(c) = False Then
                Throw New Exception("Họ và tên không được chứa kí tự khác ngoài chữ")
            End If
        Next

        If dg.dienThoai = "" Then
            Throw New Exception("Số điện thoại không được bỏ trống!")
        End If
        For Each c As Char In dg.dienThoai
            If Char.IsNumber(c) = False Then
                Throw New Exception("Số điện thoại không được chứa chữ")
            End If
        Next
        If dg.dienThoai.Length < 6 Then
            Throw New Exception("Số điện thoại quá ngắn")
        End If
        Return True
    End Function

    Public Shared Sub ThemDocGia(ByVal dg As DocGia_DTO, ByVal tk As TaiKhoan_DTO, ByVal tdg As TheDocGia_DTO, ByRef kt As Boolean)
        'Cộng thêm 1 năm để suy ra ngày hết hạn
        tdg.ngayHetHan = tdg.ngayDK.AddYears(1)

        'Mã hóa pass bằng MD5
        Dim crypt = MD5.Create()
        tk.matKhau = CreateAndCompareMD5.GetMd5Hash(crypt, tk.matKhau)

        If ktDocGiaVaTaiKhoan(dg, tk.tenTK) Then
            If DocGia_DAO.DocGia_Add(dg, tk, tdg) Then
                kt = True
            End If
        End If
    End Sub

    Shared Function ktDocGiaVaTaiKhoan(ByVal dg As DocGia_DTO, ByVal tentk As String) As Boolean
        Dim dt As DataTable = DocGia_DAO.DocGiaKTEmail(dg)
        If dt.Rows.Count = 1 Then
            Throw New Exception("Email này đã tồn tại!")
        End If

        dt = DocGia_DAO.DocGiaKTGiayTo(dg)
        If dt.Rows.Count = 1 Then
            Throw New Exception("Giấy tờ tùy thân đã tồn tại!")
        End If

        dt = TaiKhoan_DAO.KiemTraTaiKhoan(tentk)
        If dt.Rows.Count = 1 Then
            Throw New Exception("Tên tài khoản này đã có người đăng ký")
        End If

        Return True
    End Function


    Public Shared Function getInfoCardLibrary(ByVal tentk As String) As DataTable
        Return DocGia_DAO.getInfoCardLibrary(tentk)
    End Function

End Class
